# importing required libraries
import numpy as np
import pandas as pd
import plotly
# importing csv files as pandas dataframes
races = pd.read_csv("F1_data/races.csv")
results = pd.read_csv("F1_data/results.csv")
drivers = pd.read_csv("F1_data/drivers.csv")
display(races)
display(results)
display(drivers)
| raceId | year | round | circuitId | name | date | time | url | fp1_date | fp1_time | fp2_date | fp2_time | fp3_date | fp3_time | quali_date | quali_time | sprint_date | sprint_time | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2009 | 1 | 1 | Australian Grand Prix | 2009-03-29 | 06:00:00 | http://en.wikipedia.org/wiki/2009_Australian_G... | \N | \N | \N | \N | \N | \N | \N | \N | \N | \N |
| 1 | 2 | 2009 | 2 | 2 | Malaysian Grand Prix | 2009-04-05 | 09:00:00 | http://en.wikipedia.org/wiki/2009_Malaysian_Gr... | \N | \N | \N | \N | \N | \N | \N | \N | \N | \N |
| 2 | 3 | 2009 | 3 | 17 | Chinese Grand Prix | 2009-04-19 | 07:00:00 | http://en.wikipedia.org/wiki/2009_Chinese_Gran... | \N | \N | \N | \N | \N | \N | \N | \N | \N | \N |
| 3 | 4 | 2009 | 4 | 3 | Bahrain Grand Prix | 2009-04-26 | 12:00:00 | http://en.wikipedia.org/wiki/2009_Bahrain_Gran... | \N | \N | \N | \N | \N | \N | \N | \N | \N | \N |
| 4 | 5 | 2009 | 5 | 4 | Spanish Grand Prix | 2009-05-10 | 12:00:00 | http://en.wikipedia.org/wiki/2009_Spanish_Gran... | \N | \N | \N | \N | \N | \N | \N | \N | \N | \N |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1074 | 1092 | 2022 | 18 | 22 | Japanese Grand Prix | 2022-10-09 | 05:00:00 | http://en.wikipedia.org/wiki/2022_Japanese_Gra... | 2022-10-07 | 04:00:00 | 2022-10-07 | 08:00:00 | 2022-10-08 | 04:00:00 | 2022-10-08 | 07:00:00 | \N | \N |
| 1075 | 1093 | 2022 | 19 | 69 | United States Grand Prix | 2022-10-23 | 19:00:00 | http://en.wikipedia.org/wiki/2022_United_State... | 2022-10-21 | 19:00:00 | 2022-10-21 | 22:00:00 | 2022-10-22 | 19:00:00 | 2022-10-22 | 22:00:00 | \N | \N |
| 1076 | 1094 | 2022 | 20 | 32 | Mexico City Grand Prix | 2022-10-30 | 20:00:00 | http://en.wikipedia.org/wiki/2022_Mexican_Gran... | 2022-10-28 | 18:00:00 | 2022-10-28 | 21:00:00 | 2022-10-29 | 17:00:00 | 2022-10-29 | 20:00:00 | \N | \N |
| 1077 | 1095 | 2022 | 21 | 18 | Brazilian Grand Prix | 2022-11-13 | 18:00:00 | http://en.wikipedia.org/wiki/2022_Brazilian_Gr... | 2022-11-11 | 15:30:00 | 2022-11-12 | 15:30:00 | \N | \N | 2022-11-11 | 19:00:00 | 2022-11-12 | 19:30:00 |
| 1078 | 1096 | 2022 | 22 | 24 | Abu Dhabi Grand Prix | 2022-11-20 | 13:00:00 | http://en.wikipedia.org/wiki/2022_Abu_Dhabi_Gr... | 2022-11-18 | 09:00:00 | 2022-11-18 | 12:00:00 | 2022-11-19 | 10:00:00 | 2022-11-19 | 13:00:00 | \N | \N |
1079 rows × 18 columns
| resultId | raceId | driverId | constructorId | number | grid | position | positionText | positionOrder | points | laps | time | milliseconds | fastestLap | rank | fastestLapTime | fastestLapSpeed | statusId | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 18 | 1 | 1 | 22 | 1 | 1 | 1 | 1 | 10.0 | 58 | 1:34:50.616 | 5690616 | 39 | 2 | 1:27.452 | 218.300 | 1 |
| 1 | 2 | 18 | 2 | 2 | 3 | 5 | 2 | 2 | 2 | 8.0 | 58 | +5.478 | 5696094 | 41 | 3 | 1:27.739 | 217.586 | 1 |
| 2 | 3 | 18 | 3 | 3 | 7 | 7 | 3 | 3 | 3 | 6.0 | 58 | +8.163 | 5698779 | 41 | 5 | 1:28.090 | 216.719 | 1 |
| 3 | 4 | 18 | 4 | 4 | 5 | 11 | 4 | 4 | 4 | 5.0 | 58 | +17.181 | 5707797 | 58 | 7 | 1:28.603 | 215.464 | 1 |
| 4 | 5 | 18 | 5 | 1 | 23 | 3 | 5 | 5 | 5 | 4.0 | 58 | +18.014 | 5708630 | 43 | 1 | 1:27.418 | 218.385 | 1 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 25655 | 25661 | 1086 | 825 | 210 | 20 | 13 | 16 | 16 | 16 | 0.0 | 69 | \N | \N | 37 | 15 | 1:23.511 | 188.856 | 11 |
| 25656 | 25662 | 1086 | 848 | 3 | 23 | 17 | 17 | 17 | 17 | 0.0 | 69 | \N | \N | 43 | 12 | 1:23.047 | 189.911 | 11 |
| 25657 | 25663 | 1086 | 849 | 3 | 6 | 19 | 18 | 18 | 18 | 0.0 | 69 | \N | \N | 60 | 8 | 1:22.478 | 191.221 | 11 |
| 25658 | 25664 | 1086 | 852 | 213 | 22 | 16 | 19 | 19 | 19 | 0.0 | 68 | \N | \N | 58 | 16 | 1:23.538 | 188.795 | 12 |
| 25659 | 25665 | 1086 | 822 | 51 | 77 | 8 | 20 | 20 | 20 | 0.0 | 65 | \N | \N | 60 | 19 | 1:24.002 | 187.752 | 131 |
25660 rows × 18 columns
| driverId | driverRef | number | code | forename | surname | dob | nationality | url | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | hamilton | 44 | HAM | Lewis | Hamilton | 1985-01-07 | British | http://en.wikipedia.org/wiki/Lewis_Hamilton |
| 1 | 2 | heidfeld | \N | HEI | Nick | Heidfeld | 1977-05-10 | German | http://en.wikipedia.org/wiki/Nick_Heidfeld |
| 2 | 3 | rosberg | 6 | ROS | Nico | Rosberg | 1985-06-27 | German | http://en.wikipedia.org/wiki/Nico_Rosberg |
| 3 | 4 | alonso | 14 | ALO | Fernando | Alonso | 1981-07-29 | Spanish | http://en.wikipedia.org/wiki/Fernando_Alonso |
| 4 | 5 | kovalainen | \N | KOV | Heikki | Kovalainen | 1981-10-19 | Finnish | http://en.wikipedia.org/wiki/Heikki_Kovalainen |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 849 | 851 | aitken | 89 | AIT | Jack | Aitken | 1995-09-23 | British | http://en.wikipedia.org/wiki/Jack_Aitken |
| 850 | 852 | tsunoda | 22 | TSU | Yuki | Tsunoda | 2000-05-11 | Japanese | http://en.wikipedia.org/wiki/Yuki_Tsunoda |
| 851 | 853 | mazepin | 9 | MAZ | Nikita | Mazepin | 1999-03-02 | Russian | http://en.wikipedia.org/wiki/Nikita_Mazepin |
| 852 | 854 | mick_schumacher | 47 | MSC | Mick | Schumacher | 1999-03-22 | German | http://en.wikipedia.org/wiki/Mick_Schumacher |
| 853 | 855 | zhou | 24 | ZHO | Guanyu | Zhou | 1999-05-30 | Chinese | http://en.wikipedia.org/wiki/Guanyu_Zhou |
854 rows × 9 columns
What are the maximum number of wins by a single driver during each F1 season?
# creating subsets of columns with the required columns
races_subset = races[['raceId', 'year']]
display(races_subset.raceId.unique())
display(races_subset.year.unique())
array([ 1, 2, 3, ..., 1094, 1095, 1096], dtype=int64)
array([2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999,
1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 2010, 1989,
1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978,
1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967,
1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956,
1955, 1954, 1953, 1952, 1951, 1950, 2011, 2012, 2013, 2014, 2015,
2016, 2017, 2018, 2019, 2020, 2021, 2022], dtype=int64)
# creating subsets of columns with the required columns
results_subset = results[['resultId','driverId','raceId','positionText']]
display(results_subset.resultId.unique())
display(results_subset.driverId.unique())
display(results_subset.raceId.unique())
display(results_subset.positionText.unique())
array([ 1, 2, 3, ..., 25663, 25664, 25665], dtype=int64)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 56, 63, 62, 59, 66, 54, 55, 57, 58, 60, 61, 64,
65, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
106, 107, 108, 110, 109, 111, 112, 113, 114, 115, 116, 117, 118,
119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
132, 135, 136, 137, 138, 139, 133, 140, 141, 142, 143, 144, 145,
146, 147, 148, 151, 149, 150, 152, 67, 153, 154, 155, 156, 157,
158, 159, 163, 160, 161, 162, 164, 134, 165, 166, 167, 168, 169,
170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,
196, 197, 198, 199, 200, 206, 201, 202, 203, 204, 205, 207, 208,
209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,
235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260,
261, 262, 263, 267, 264, 265, 266, 268, 269, 270, 271, 272, 273,
274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286,
287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299,
300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312,
314, 313, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325,
326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338,
339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351,
352, 353, 354, 355, 356, 357, 362, 358, 359, 360, 361, 363, 364,
365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377,
378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390,
391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403,
404, 405, 406, 408, 407, 409, 410, 411, 412, 413, 414, 415, 416,
417, 418, 420, 419, 421, 422, 423, 424, 425, 426, 427, 428, 429,
430, 431, 432, 433, 434, 435, 440, 436, 437, 438, 439, 441, 442,
443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455,
456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468,
469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481,
482, 483, 485, 484, 487, 488, 489, 490, 486, 491, 492, 493, 494,
495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507,
508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520,
521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533,
534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546,
547, 548, 554, 549, 550, 551, 552, 553, 555, 556, 557, 558, 559,
560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572,
573, 574, 575, 576, 577, 578, 579, 580, 581, 590, 582, 583, 584,
585, 586, 587, 588, 589, 591, 592, 593, 594, 595, 596, 597, 598,
599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611,
612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624,
625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637,
638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650,
651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663,
664, 666, 665, 667, 668, 669, 670, 671, 672, 673, 674, 675, 679,
676, 677, 678, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689,
690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702,
703, 704, 706, 707, 708, 705, 710, 709, 711, 712, 713, 714, 715,
716, 717, 718, 720, 719, 721, 722, 723, 725, 724, 726, 727, 728,
729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 741, 740,
742, 743, 744, 745, 746, 747, 748, 749, 751, 752, 753, 750, 754,
755, 756, 757, 758, 759, 762, 761, 760, 763, 764, 765, 766, 767,
768, 769, 770, 771, 772, 773, 786, 774, 775, 778, 776, 777, 779,
780, 781, 783, 782, 787, 784, 785, 789, 790, 793, 792, 794, 795,
796, 797, 798, 799, 800, 801, 802, 791, 788, 803, 804, 805, 806,
807, 811, 808, 810, 812, 814, 816, 813, 815, 817, 818, 819, 821,
822, 824, 820, 823, 825, 826, 828, 827, 829, 831, 832, 830, 833,
834, 835, 836, 837, 838, 839, 841, 840, 842, 843, 844, 845, 846,
848, 847, 849, 851, 850, 852, 854, 853, 855], dtype=int64)
array([ 18, 19, 20, ..., 1084, 1085, 1086], dtype=int64)
array(['1', '2', '3', '4', '5', '6', '7', '8', 'R', 'D', '9', '10', '11',
'12', '13', '14', '15', '16', '17', '18', '19', '20', '21', 'N',
'W', 'F', 'E', '22', '23', '24', '25', '26', '27', '28', '29',
'30', '31', '32', '33'], dtype=object)
# Cleaning result file
results_cleaned=results_subset.loc[(results_subset['positionText'] != 'R') & (results_subset['positionText'] != 'N' )
& (results_subset['positionText'] != 'E' ) & (results_subset['positionText'] != 'W' )& (results_subset['positionText'] != 'D')
& (results_subset['positionText'] != 'F' )]
display(results_cleaned.positionText.unique())
array(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
'13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23',
'24', '25', '26', '27', '28', '29', '30', '31', '32', '33'],
dtype=object)
# Converting Dtype Object -> int
results_cleaned = results_cleaned.astype({'positionText':'int'})
print(results_cleaned.dtypes)
display(results_cleaned.positionText.unique())
resultId int64 driverId int64 raceId int64 positionText int32 dtype: object
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33])
## creating subsets of columns with the required columns
drivers_subset = drivers[['driverId', 'forename','surname']]
display(drivers_subset.driverId.unique())
display(drivers_subset.forename.unique())
display(drivers_subset.surname.unique())
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,
196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,
235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260,
261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273,
274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286,
287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299,
300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312,
313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325,
326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338,
339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351,
352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364,
365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377,
378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390,
391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403,
404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416,
417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429,
430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442,
443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 842, 453, 454,
455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467,
468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480,
481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493,
494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506,
507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519,
520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532,
533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545,
546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558,
559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571,
572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584,
585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597,
598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610,
611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623,
624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636,
637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649,
650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662,
663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675,
676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688,
689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701,
702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714,
715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727,
728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740,
741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753,
754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766,
767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779,
780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792,
793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805,
806, 807, 808, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819,
820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832,
833, 834, 835, 836, 837, 838, 839, 840, 841, 843, 844, 845, 846,
847, 848, 849, 850, 851, 852, 853, 854, 855], dtype=int64)
array(['Lewis', 'Nick', 'Nico', 'Fernando', 'Heikki', 'Kazuki',
'Sébastien', 'Kimi', 'Robert', 'Timo', 'Takuma', 'Nelson',
'Felipe', 'David', 'Jarno', 'Adrian', 'Mark', 'Jenson', 'Anthony',
'Sebastian', 'Giancarlo', 'Rubens', 'Ralf', 'Vitantonio',
'Alexander', 'Scott', 'Christijan', 'Markus', 'Sakon', 'Michael',
'Juan', 'Christian', 'Tiago', 'Yuji', 'Jacques', 'Franck', 'Pedro',
'Narain', 'Patrick', 'Ricardo', 'Antônio', 'Cristiano', 'Olivier',
'Giorgio', 'Gianmaria', 'Zsolt', 'Marc', 'Heinz-Harald', 'Jos',
'Justin', 'Ralph', 'Nicolas', 'Luciano', 'Jean', 'Eddie', 'Mika',
'Tarso', 'Enrique', 'Gastón', 'Tomáš', 'Alex', 'Johnny', 'Allan',
'Toranosuke', 'Luca', 'Alessandro', 'Damon', 'Stéphane', 'Esteban',
'Shinji', 'Jan', 'Gerhard', 'Nicola', 'Ukyo', 'Vincenzo', 'Gianni',
'Norberto', 'Martin', 'Andrea', 'Giovanni', 'Aguri', 'Taki',
'Roberto', 'Karl', 'Bertrand', 'Domenico', 'Pierluigi', 'Nigel',
'Jean-Christophe', 'Massimiliano', 'Jean-Denis', 'Gabriele',
'Érik', 'Ayrton', 'Éric', 'Michele', 'Roland', 'Paul', 'Jyrki',
'Jean-Marc', 'Philippe', 'Yannick', 'Hideki', 'Alain', 'Derek',
'Riccardo', 'Fabrizio', 'Ivan', 'Thierry', 'Marco', 'Emanuele',
'Toshio', 'Maurício', 'Eric', 'Stefano', 'Giovanna', 'Enrico',
'Perry', 'Satoru', 'Stefan', 'Julian', 'Naoki', 'Bernd', 'Paolo',
'Gregor', 'Claudio', 'Gary', 'Bruno', 'Jaime', 'Romain', 'Kamui',
'Jonathan', 'Luis', 'Piercarlo', 'Volker', 'Pierre-Henri', 'René',
'Joachim', 'Oscar', 'Adrián', 'Jean-Louis', 'Pascal', 'Teo',
'Franco', 'Elio', 'Keke', 'Alan', 'Huub', 'Allen', 'Manfred',
'Niki', 'François', 'Mauro', 'Kenny', 'John', 'Jo', 'Corrado',
'Mike', 'Chico', 'Danny', 'Eliseo', 'Raul', 'Jean-Pierre',
'Carlos', 'Jochen', 'Slim', 'Didier', 'Gilles', 'Brian', 'Mario',
'Emilio', 'Geoff', 'Tommy', 'Rupert', 'Hector', 'Beppe', 'Kevin',
'Miguel Ángel', 'Siegfried', 'Jody', 'Clay', 'Emerson', 'Dave',
'Stephen', 'Tiff', 'Desiré', 'Harald', 'Vittorio', 'James',
'Arturo', 'Hans-Joachim', 'Gianfranco', 'Jacky', 'Ronnie', 'Brett',
'Lamberto', 'Divina', 'Rolf', 'Alberto', 'Tony', 'Hans', 'Carlo',
'Bobby', 'Ian', 'Tom', 'Ingo', 'Renzo', 'Gunnar', 'Larry', 'Boy',
'Conny', 'Bernard', 'Jackie', 'Mikko', 'Andy', 'Guy', 'Vern',
'Teddy', 'Loris', 'Kunimitsu', 'Kazuyoshi', 'Noritake', 'Lella',
'Bob', 'Michel', 'Chris', 'Henri', 'Jac', 'Damien', 'Otto',
'Warwick', 'Masahiro', 'Graham', 'Wilson', 'Roelof', 'Torsten',
'Gijs', 'Hiroshi', 'Jim', 'Denny', 'Howden', 'Richard', 'Peter',
'Paddy', 'Rikky', 'Tim', 'Gérard', 'Leo', 'Reine', 'Bertil',
'José', 'Dieter', 'Helmuth', 'Eppie', 'Nanni', 'Luiz', 'George',
'Roger', 'Helmut', 'Skip', 'Bill', 'Sam', 'Max', 'Vic', 'Silvio',
'Pete', 'Jack', 'Bruce', 'Piers', 'Ignazio', 'Dan', 'Hubert',
'Gus', 'Basil', 'Al', 'Ludovico', 'Lucien', 'Robin', 'Kurt',
'Frank', 'Moisés', 'Luki', 'Lorenzo', 'Richie', 'Trevor',
'Giacomo', 'Phil', 'Innes', 'Neville', 'Doug', 'Brausch', 'Ernie',
'Clive', 'Ray', 'Masten', 'Nino', 'Maurice', 'André',
'Carel Godin', 'Edgar', 'Mário de Araújo', 'Walt', 'Hap', 'Willy',
'Nasif', 'Ernesto', 'Günther', 'Gaetano', 'Rodger', 'Thomas',
'Pierre', 'Wolfgang', 'Roy', 'Ben', 'Rob', 'Gerry', 'Heinz',
'Colin', 'Jay', 'Keith', 'Heini', 'Timmy', 'Syd', 'Stirling',
'Cliff', 'Henry', 'Juan Manuel', 'Massimo', 'Renato', 'Alfonso',
'Menato', 'Lloyd', 'Ken', 'Alberto Rodriguez', 'José Froilán',
'Gino', 'Harry', 'Ettore', 'Antonio', 'Chuck', 'Lance', 'Don',
'Bud', 'Red', 'Duane', 'Gene', 'Shorty', 'Jimmy', 'Troy', 'Wayne',
'Len', 'Dick', 'Dempsey', 'Ron', 'Giulio', 'Piero', 'Fred',
'Arthur', 'Horace', 'Ivor', 'Maria', 'Pat', 'Jud', 'Carroll',
'Fritz', 'Azdrubal', 'Dennis', 'Luigi', 'Paco', 'Gerino', 'Bernie',
'Louis', 'Stuart', 'Johnnie', 'Billy', 'Ed', 'Jerry', 'Art',
'Cesare', 'Eugenio', 'Les', 'Marshall', 'Elmer', 'Herbert',
'Umberto', 'Hernando', 'Élie', 'Duke', 'Desmond', 'Archie',
'Ottorino', 'Toulo', 'Sergio', 'Clemar', 'Jesús', 'Pablo', 'Ted',
'Cal', 'Kenneth', 'Leslie', 'Prince', 'Onofre', 'Jorge', 'Manny',
'Travis', 'Georges', 'Reg', 'Rodney', 'Hermann', 'Theo', 'Felice',
'Adolfo', 'Carl', 'Yves', 'Duncan', 'Ernst', 'Rudolf', 'Oswald',
'Willi', 'Erwin', 'Albert', 'Rudi', 'Toni', 'Joe', 'Chet',
'Charles', 'Eitel', 'Adolf', 'Marcel', 'Josef', 'Dries',
'Consalvo', 'Lee', 'Mauri', 'Cecil', 'Mack', 'Eugène', 'Aldo',
'Philip', 'Cuth', 'Clemente', 'Alfredo', 'Raymond', 'Joie',
'Myron', 'Bayliss', 'Nello', 'Dorino', 'Óscar', 'Vitaly', 'Lucas',
'Karun', 'Pastor', 'Jérôme', 'Daniel', 'Jean-Éric', 'Valtteri',
'Giedo', 'Jules', 'Daniil', 'Marcus', 'Will', 'Jolyon', 'Rio',
'Stoffel', 'Brendon', 'Sergey', 'Lando', 'Nicholas', 'Pietro',
'Yuki', 'Nikita', 'Mick', 'Guanyu'], dtype=object)
array(['Hamilton', 'Heidfeld', 'Rosberg', 'Alonso', 'Kovalainen',
'Nakajima', 'Bourdais', 'Räikkönen', 'Kubica', 'Glock', 'Sato',
'Piquet Jr.', 'Massa', 'Coulthard', 'Trulli', 'Sutil', 'Webber',
'Button', 'Davidson', 'Vettel', 'Fisichella', 'Barrichello',
'Schumacher', 'Liuzzi', 'Wurz', 'Speed', 'Albers', 'Winkelhock',
'Yamamoto', 'Pablo Montoya', 'Klien', 'Monteiro', 'Ide',
'Villeneuve', 'Montagny', 'de la Rosa', 'Doornbos', 'Karthikeyan',
'Friesacher', 'Zonta', 'Pizzonia', 'da Matta', 'Panis', 'Pantano',
'Bruni', 'Baumgartner', 'Gené', 'Frentzen', 'Verstappen', 'Wilson',
'Firman', 'Kiesa', 'Burti', 'Alesi', 'Irvine', 'Häkkinen',
'Marques', 'Bernoldi', 'Mazzacane', 'Enge', 'Yoong', 'Salo',
'Diniz', 'Herbert', 'McNish', 'Buemi', 'Takagi', 'Badoer',
'Zanardi', 'Hill', 'Sarrazin', 'Rosset', 'Tuero', 'Nakano',
'Magnussen', 'Berger', 'Larini', 'Katayama', 'Sospiri',
'Morbidelli', 'Fontana', 'Lamy', 'Brundle', 'Montermini',
'Lavaggi', 'Blundell', 'Suzuki', 'Inoue', 'Moreno', 'Wendlinger',
'Gachot', 'Schiattarella', 'Martini', 'Mansell', 'Boullion',
'Papis', 'Délétraz', 'Tarquini', 'Comas', 'Brabham', 'Senna',
'Bernard', 'Fittipaldi', 'Alboreto', 'Beretta', 'Ratzenberger',
'Belmondo', 'Järvilehto', 'de Cesaris', 'Gounon', 'Alliot',
'Adams', 'Dalmas', 'Noda', 'Lagorce', 'Prost', 'Warwick',
'Patrese', 'Barbazza', 'Andretti', 'Capelli', 'Boutsen',
'Apicella', 'Naspetti', 'Gugelmin', 'van de Poele', 'Grouillard',
'Chiesa', 'Modena', 'Amati', 'Caffi', 'Bertaggia', 'McCarthy',
'Lammers', 'Piquet', 'Pirro', 'Johansson', 'Bailey', 'Chaves',
'Bartels', 'Hattori', 'Nannini', 'Schneider', 'Barilla', 'Foitek',
'Langes', 'Donnelly', 'Giacomelli', 'Alguersuari', 'Grosjean',
'Kobayashi', 'Palmer', 'Danner', 'Cheever', 'Pérez-Sala',
'Ghinzani', 'Weidler', 'Raphanel', 'Arnoux', 'Larrauri', 'Streiff',
'Campos', 'Schlesser', 'Fabre', 'Fabi', 'Forini', 'Laffite',
'de Angelis', 'Dumfries', 'Tambay', 'Surer', 'Jones',
'Rothengatter', 'Berg', 'Lauda', 'Hesnault', 'Baldi', 'Bellof',
'Acheson', 'Watson', 'Cecotto', 'Gartner', 'Thackwell', 'Serra',
'Sullivan', 'Salazar', 'Guerrero', 'Boesel', 'Jarier',
'Villeneuve Sr.', 'Reutemann', 'Mass', 'Borgudd', 'Pironi',
'Paletti', 'Henton', 'Daly', 'de Villota', 'Lees', 'Byrne',
'Keegan', 'Rebaque', 'Gabbiani', 'Cogan', 'Guerra', 'Stohr',
'Zunino', 'Londoño', 'Jabouille', 'Francia', 'Depailler',
'Scheckter', 'Regazzoni', 'Kennedy', 'South', 'Needell', 'Ertl',
'Brambilla', 'Hunt', 'Merzario', 'Stuck', 'Brancatelli', 'Ickx',
'Gaillard', 'Ribeiro', 'Peterson', 'Lunger', 'Ongais', 'Leoni',
'Galica', 'Stommelen', 'Colombo', 'Trimmer', 'Binder',
'Bleekemolen', 'Franchi', 'Rahal', 'Pace', 'Pryce', 'Hoffmann',
'Zorzi', 'Nilsson', 'Perkins', 'Nève', 'Purley', 'Andersson',
'de Dryver', 'Oliver', 'Kozarowitzky', 'Sutcliffe', 'Edwards',
'McGuire', 'Schuppan', 'Heyer', 'Pilette', 'Ashley', 'Kessel',
'Takahashi', 'Hoshino', 'Takahara', 'Lombardi', 'Evans', 'Leclère',
'Amon', 'Zapico', 'Pescarolo', 'Nelleman', 'Magee', 'Wilds',
'Pesenti-Rossi', 'Stuppacher', 'Brown', 'Hasemi', 'Donohue',
'Tunmer', 'Keizan', 'Charlton', 'Brise', 'Wunderink', 'Migault',
'Palm', 'van Lennep', 'Fushida', 'Nicholson', 'Morgan', 'Crawford',
'Vonlanthen', 'Hulme', 'Hailwood', 'Beltoise', 'Ganley', 'Robarts',
'Revson', 'Driver', 'Belsø', 'Redman', 'von Opel', 'Schenken',
'Larrousse', 'Kinnunen', 'Wisell', 'Roos', 'Dolhem', 'Gethin',
'Bell', 'Hobbs', 'Quester', 'Koinigg', 'Facetti', 'Wietzes',
'Cevert', 'Stewart', 'Beuttler', 'Galli', 'Bueno', 'Follmer',
'de Adamich', 'Pretorius', 'Williamson', 'McRae', 'Marko',
'Walker', 'Soler-Roig', 'Love', 'Surtees', 'Barber', 'Brack',
'Posey', 'Rodríguez', 'Siffert', 'Bonnier', 'Mazet', 'Jean',
'Elford', 'Moser', 'Eaton', 'Lovely', 'Craft', 'Cannon', 'Miles',
'Rindt', 'Servoz-Gavin', 'McLaren', 'Courage', 'de Klerk',
'Giunti', 'Gurney', 'Hahne', 'Hutchison', 'Westbury', 'Tingle',
'van Rooyen', 'Attwood', 'Pease', 'Cordts', 'Clark', 'Spence',
'Scarfiotti', 'Bianchi', 'Widdows', 'Ahrens', 'Gardner', 'Unser',
'Solana', 'Anderson', 'Botha', 'Bandini', 'Ginther', 'Parkes',
'Irwin', 'Ligier', 'Rees', 'Hart', 'Fisher', 'Baghetti',
'Williams', 'Bondurant', 'Arundell', 'Taylor', 'Lawrence', 'Russo',
'Ireland', 'Bucknum', 'Hawkins', 'Prophet', 'Maggs', 'Blokdyk',
'Lederle', 'Serrurier', 'Niemann', 'Pieterse', 'Puzey', 'Reed',
'Clapham', 'Blignaut', 'Gregory', 'Rhodes', 'Raby', 'Rollinson',
'Gubby', 'Mitter', 'Bussinello', 'Vaccarella', 'Bassi',
'Trintignant', 'Collomb', 'de Beaufort', 'Barth', 'Cabral',
'Hansgen', 'Sharp', 'Mairesse', 'Campbell-Jones', 'Burgess',
'Settember', 'Estéfano', 'Hall', 'Parnell', 'Kuhnke', 'Lippi',
'Seiffert', 'Abate', 'Starrabba', 'Broeker', 'Ward', 'de Vos',
'Dochnal', 'Monarch', 'Gasly', 'Lewis', 'Seidel', 'Salvadori',
'Pon', 'Slotemaker', 'Marsh', 'Ashmore', 'Schiller', 'Davis',
'Chamberlain', 'Shelly', 'Greene', 'Walter', 'Prinoth', 'Penske',
'Schroeder', 'Mayer', 'Johnstone', 'Harris', 'Hocking',
'van der Vyver', 'Moss', 'von Trips', 'Allison', 'Herrmann',
'Brooks', 'May', 'Gendebien', 'Scarlatti', 'Naylor', 'Bordeu',
'Fairman', 'Natili', 'Monteverdi', 'Pirocchi', 'Duke', 'Thiele',
'Boffa', 'Ryan', 'Ruby', 'Menditeguy', 'Larreta', 'González',
'Bonomi', 'Munaron', 'Schell', 'Stacey', 'Chimeri', 'Creus',
'Bristow', 'Halford', 'Daigh', 'Reventlow', 'Rathmann',
'Goldsmith', 'Branson', 'Thomson', 'Johnson', 'Veith',
'Tingelstad', 'Christie', 'Amick', 'Carter', 'Homeier', 'Hartley',
'Stevenson', 'Grim', 'Templeman', 'Hurtubise', 'Bryan', 'Ruttman',
'Sachs', 'Freeland', 'Bettenhausen', 'Weiler', 'Foyt', 'Boyd',
'Force', 'McWithey', 'Sutton', 'Herman', 'Flockhart', 'Piper',
'Cabianca', 'Drogo', 'Gamble', 'Owen', 'Gould', 'Drake', 'Bueb',
'de Changy', 'de Filippis', 'Lucienbonnet', 'Testut', 'Behra',
'Daywalt', 'Arnold', 'Keller', 'Flaherty', 'Cheesbourg', 'Turner',
'Weyant', 'Larson', 'Magill', 'Shelby', "d'Orey", 'Fontes',
'Ashdown', 'Blanchard', 'de Tomaso', 'Constantine', 'Said', 'Cade',
'Musso', 'Hawthorn', 'Fangio', 'Godia', 'Collins', 'Kavanagh',
'Gerini', 'Kessler', 'Emery', 'Piotti', 'Ecclestone', 'Taramazzo',
'Chiron', 'Lewis-Evans', 'Reece', 'Parsons', 'Tolan', 'Garrett',
'Elisian', "O'Connor", 'Bisch', 'Goethals', 'Gibson', 'La Caze',
'Guelfi', 'Picard', 'Bridger', 'de Portago', 'Perdisa',
'Castellotti', 'Simon', 'Leston', 'Hanks', 'Linden', 'Teague',
'Edmunds', 'Agabashian', 'George', 'MacDowel', 'MacKay-Fraser',
'Gerard', 'Maglioli', 'England', 'Landi', 'Uria', 'da Silva Ramos',
'Bayol', 'Manzon', 'Rosier', 'Sweikert', 'Griffith', 'Dinsmore',
'Andrews', 'Frère', 'Villoresi', 'Scotti', 'Chapman',
'Titterington', 'Scott Brown', 'Volonterio', 'Milhoux',
'de Graffenried', 'Taruffi', 'Farina', 'Mieres', 'Mantovani',
'Bucci', 'Iglesias', 'Ascari', 'Kling', 'Birger', 'Pollet',
'Macklin', 'Whiteaway', 'Davies', 'Faulkner', 'Niday', 'Cross',
'Vukovich', 'McGrath', 'Hoyt', 'Claes', 'Sparken', 'Wharton',
'McAlpine', 'Marr', 'Rolt', 'Fitch', 'Lucas', 'Bira', 'Marimón',
'Loyer', 'Daponte', 'Nazaruk', 'Crockett', 'Ayulo', 'Armi', 'Webb',
'Duncan', 'McCoy', 'Swaters', 'Beauman', 'Thorne', 'Whitehouse',
'Riseley-Prichard', 'Whitehead', 'Brandon', 'Nuckey', 'Lang',
'Helfrich', 'Wacker', 'de Riu', 'Gálvez', 'Bonetto', 'Cruz',
'Nalon', 'Scarborough', 'Holland', 'Scott', 'Legat', 'Cabantous',
'Crook', 'Klodwig', 'Krause', 'Karch', 'Heeks', 'Fitzau', 'Adolff',
'Bechem', 'Bauer', 'von Stuck', 'Loof', 'Scherrer', 'de Terra',
'Hirt', 'Carini', 'Fischer', 'Ulmen', 'Abecassis', 'Connor',
'Rigsby', 'James', 'Schindler', 'Fonder', 'Banks', 'McDowell',
'Miller', 'Ball', 'de Tornaco', 'Laurent', "O'Brien", 'Gaze',
'Montgomerie-Charrington', 'Comotti', 'Étancelin', 'Poore',
'Thompson', 'Downing', 'Bianco', 'Murray', 'Cantoni', 'Aston',
'Brudes', 'Riess', 'Niedermayr', 'Klenk', 'Balsa', 'Schoeller',
'Pietsch', 'Peters', 'van der Lof', 'Flinterman', 'Dusio',
'Crespo', 'Rol', 'Sanesi', 'Louveau', 'Wallard', 'Forberg', 'Rose',
'Mackey', 'Green', 'Hellings', 'Levegh', 'Chaboud', 'Gordini',
'Kelly', 'Fotheringham-Parker', 'Shawe Taylor', 'Branca',
'Richardson', 'Jover', 'Grignard', 'Hampshire', 'Crossley',
'Fagioli', 'Harrison', 'Fry', 'Martin', 'Biondetti', 'Pián',
'Sommer', 'Chitwood', 'Fohr', 'Ader', 'Holmes', 'Levrett',
'Jackson', 'Pagani', 'Pozzi', 'Serafini', 'Cantrell', 'Mantz',
'Kladis', 'Hülkenberg', 'Petrov', 'di Grassi', 'Chandhok',
'Maldonado', 'di Resta', 'Pérez', "d'Ambrosio", 'Ricciardo',
'Vergne', 'Pic', 'Chilton', 'Gutiérrez', 'Bottas', 'van der Garde',
'Kvyat', 'Lotterer', 'Ericsson', 'Stevens', 'Nasr', 'Sainz',
'Merhi', 'Rossi', 'Wehrlein', 'Haryanto', 'Vandoorne', 'Ocon',
'Stroll', 'Giovinazzi', 'Leclerc', 'Sirotkin', 'Norris', 'Russell',
'Albon', 'Latifi', 'Aitken', 'Tsunoda', 'Mazepin', 'Zhou'],
dtype=object)
## joining the 2 datasets to create a final database for analysis
Results_races_combined=pd.merge(results_cleaned,races_subset,on='raceId',how='left')
Final_Dataset=pd.merge(Results_races_combined,drivers_subset,on='driverId',how='left')
display(Final_Dataset)
| resultId | driverId | raceId | positionText | year | forename | surname | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 1 | 18 | 1 | 2008 | Lewis | Hamilton |
| 1 | 2 | 2 | 18 | 2 | 2008 | Nick | Heidfeld |
| 2 | 3 | 3 | 18 | 3 | 2008 | Nico | Rosberg |
| 3 | 4 | 4 | 18 | 4 | 2008 | Fernando | Alonso |
| 4 | 5 | 5 | 18 | 5 | 2008 | Heikki | Kovalainen |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 14830 | 25661 | 825 | 1086 | 16 | 2022 | Kevin | Magnussen |
| 14831 | 25662 | 848 | 1086 | 17 | 2022 | Alexander | Albon |
| 14832 | 25663 | 849 | 1086 | 18 | 2022 | Nicholas | Latifi |
| 14833 | 25664 | 852 | 1086 | 19 | 2022 | Yuki | Tsunoda |
| 14834 | 25665 | 822 | 1086 | 20 | 2022 | Valtteri | Bottas |
14835 rows × 7 columns
# reading file and making subsets for the required columns
pd.set_option('mode.chained_assignment', None) # remove warnings
lap_times = pd.read_csv("F1_data/lap_times.csv")
lap_times_subset = lap_times[['raceId','driverId', 'lap','milliseconds']]
# extracting the best driver based on the fastest lap time
pd.set_option('mode.chained_assignment', None) # remove warnings
lap_times_subset['rank'] = lap_times_subset.groupby(['raceId','driverId'])['milliseconds'].rank(method='first')
lap_times_filtered=lap_times_subset.loc[(lap_times_subset['rank'] == 1)]
lap_times_filtered
| raceId | driverId | lap | milliseconds | rank | |
|---|---|---|---|---|---|
| 43 | 841 | 20 | 44 | 89844 | 1.0 |
| 98 | 841 | 1 | 41 | 90314 | 1.0 |
| 165 | 841 | 17 | 50 | 89600 | 1.0 |
| 228 | 841 | 808 | 55 | 90064 | 1.0 |
| 286 | 841 | 13 | 55 | 88947 | 1.0 |
| ... | ... | ... | ... | ... | ... |
| 528498 | 1086 | 852 | 58 | 83538 | 1.0 |
| 528566 | 1086 | 855 | 58 | 82029 | 1.0 |
| 528628 | 1086 | 20 | 51 | 82824 | 1.0 |
| 528706 | 1086 | 849 | 60 | 82478 | 1.0 |
| 528773 | 1086 | 842 | 58 | 83199 | 1.0 |
9971 rows × 5 columns
# Filtering the Data to get only winners
Winners_data=Final_Dataset.loc[(Final_Dataset['positionText'] == 1)]
Winners_data = Winners_data.merge(lap_times_filtered, on=['raceId','driverId'], how='left')
display(Winners_data)
| resultId | driverId | raceId | positionText | year | forename | surname | lap | milliseconds | rank | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 1 | 18 | 1 | 2008 | Lewis | Hamilton | 39.0 | 87452.0 | 1.0 |
| 1 | 23 | 8 | 19 | 1 | 2008 | Kimi | Räikkönen | 37.0 | 95405.0 | 1.0 |
| 2 | 45 | 13 | 20 | 1 | 2008 | Felipe | Massa | 38.0 | 93600.0 | 1.0 |
| 3 | 67 | 8 | 21 | 1 | 2008 | Kimi | Räikkönen | 46.0 | 81670.0 | 1.0 |
| 4 | 89 | 13 | 22 | 1 | 2008 | Felipe | Massa | 16.0 | 86666.0 | 1.0 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 1068 | 25566 | 830 | 1082 | 1 | 2022 | Max | Verstappen | 64.0 | 75839.0 | 1.0 |
| 1069 | 25586 | 832 | 1083 | 1 | 2022 | Carlos | Sainz | 44.0 | 90813.0 | 1.0 |
| 1070 | 25606 | 844 | 1084 | 1 | 2022 | Charles | Leclerc | 62.0 | 67583.0 | 1.0 |
| 1071 | 25626 | 830 | 1085 | 1 | 2022 | Max | Verstappen | 30.0 | 97491.0 | 1.0 |
| 1072 | 25646 | 830 | 1086 | 1 | 2022 | Max | Verstappen | 45.0 | 82126.0 | 1.0 |
1073 rows × 10 columns
# Filtering data from 1996 onwards,since we have data for lap times starting 1996
average_lap_times = pd.DataFrame(Winners_data.groupby(['year','driverId'], as_index = False)['milliseconds'].mean())
average_lap_times=average_lap_times.loc[(average_lap_times['year'] >= 1996)]
average_lap_times
| year | driverId | milliseconds | |
|---|---|---|---|
| 256 | 1996 | 30 | 101844.000000 |
| 257 | 1996 | 35 | 83689.500000 |
| 258 | 1996 | 44 | 85581.000000 |
| 259 | 1996 | 71 | 90738.000000 |
| 260 | 1997 | 14 | 88693.500000 |
| ... | ... | ... | ... |
| 387 | 2021 | 839 | 81421.000000 |
| 388 | 2022 | 815 | 76028.000000 |
| 389 | 2022 | 830 | 88567.625000 |
| 390 | 2022 | 832 | 90813.000000 |
| 391 | 2022 | 844 | 80804.333333 |
136 rows × 3 columns
# Summarizing data to get the number of win by each driver across all seasons
yearly_drivers_position = pd.DataFrame(Winners_data.groupby(['year','driverId'], as_index = False)['positionText'].count())
yearly_drivers_position=yearly_drivers_position.loc[(yearly_drivers_position['year'] >= 1996)]
yearly_drivers_position = yearly_drivers_position.rename(columns={'positionText':'races_won'})
display(yearly_drivers_position)
type(yearly_drivers_position)
| year | driverId | races_won | |
|---|---|---|---|
| 256 | 1996 | 30 | 3 |
| 257 | 1996 | 35 | 4 |
| 258 | 1996 | 44 | 1 |
| 259 | 1996 | 71 | 8 |
| 260 | 1997 | 14 | 2 |
| ... | ... | ... | ... |
| 387 | 2021 | 839 | 1 |
| 388 | 2022 | 815 | 1 |
| 389 | 2022 | 830 | 8 |
| 390 | 2022 | 832 | 1 |
| 391 | 2022 | 844 | 3 |
136 rows × 3 columns
pandas.core.frame.DataFrame
## joining the 2 datasets to create a final database for analysis
yearly_drivers_position_lap=pd.merge(yearly_drivers_position,average_lap_times,on=['year','driverId'],how='left')
yearly_drivers_position_lap
| year | driverId | races_won | milliseconds | |
|---|---|---|---|---|
| 0 | 1996 | 30 | 3 | 101844.000000 |
| 1 | 1996 | 35 | 4 | 83689.500000 |
| 2 | 1996 | 44 | 1 | 85581.000000 |
| 3 | 1996 | 71 | 8 | 90738.000000 |
| 4 | 1997 | 14 | 2 | 88693.500000 |
| ... | ... | ... | ... | ... |
| 131 | 2021 | 839 | 1 | 81421.000000 |
| 132 | 2022 | 815 | 1 | 76028.000000 |
| 133 | 2022 | 830 | 8 | 88567.625000 |
| 134 | 2022 | 832 | 1 | 90813.000000 |
| 135 | 2022 | 844 | 3 | 80804.333333 |
136 rows × 4 columns
# filtering the data to only get the drivers with the maximum wins in a season
yearly_drivers_position_lap['count_max'] = yearly_drivers_position_lap.groupby(['year'])['races_won'].transform(max)
yearly_drivers_position2 = yearly_drivers_position_lap[(yearly_drivers_position_lap['races_won']==yearly_drivers_position_lap['count_max'])]
yearly_drivers_position2 = yearly_drivers_position2[['year','driverId','races_won','milliseconds']]
yearly_drivers_position2
| year | driverId | races_won | milliseconds | |
|---|---|---|---|---|
| 3 | 1996 | 71 | 8 | 90738.000000 |
| 6 | 1997 | 35 | 7 | 80679.857143 |
| 12 | 1998 | 57 | 8 | 87343.625000 |
| 18 | 1999 | 57 | 5 | 85398.200000 |
| 22 | 2000 | 30 | 9 | 86048.111111 |
| 26 | 2001 | 30 | 9 | 87799.222222 |
| 32 | 2002 | 30 | 11 | 83060.727273 |
| 39 | 2003 | 30 | 6 | 76803.000000 |
| 44 | 2004 | 30 | 13 | 81564.076923 |
| 46 | 2005 | 4 | 7 | 86675.571429 |
| 47 | 2005 | 8 | 7 | 85411.571429 |
| 51 | 2006 | 4 | 7 | 83043.285714 |
| 55 | 2006 | 30 | 7 | 83434.000000 |
| 58 | 2007 | 8 | 6 | 86817.500000 |
| 65 | 2008 | 13 | 6 | 89610.166667 |
| 70 | 2009 | 18 | 6 | 87486.166667 |
| 74 | 2010 | 4 | 5 | 99307.800000 |
| 77 | 2010 | 20 | 5 | 93325.800000 |
| 82 | 2011 | 20 | 11 | 94468.272727 |
| 89 | 2012 | 20 | 5 | 99101.800000 |
| 95 | 2013 | 20 | 13 | 95295.230769 |
| 96 | 2014 | 1 | 11 | 100653.090909 |
| 99 | 2015 | 1 | 10 | 96220.700000 |
| 102 | 2016 | 1 | 10 | 85645.500000 |
| 106 | 2017 | 1 | 9 | 92422.000000 |
| 111 | 2018 | 1 | 11 | 89360.818182 |
| 116 | 2019 | 1 | 11 | 86550.727273 |
| 121 | 2020 | 1 | 11 | 84877.545455 |
| 130 | 2021 | 830 | 10 | 92733.800000 |
| 133 | 2022 | 830 | 8 | 88567.625000 |
# extracting the best driver based on the fastest lap time
yearly_drivers_position2['rank'] = yearly_drivers_position2.groupby('year')['milliseconds'].rank(method='first')
yearly_drivers_position2=yearly_drivers_position2.loc[(yearly_drivers_position2['rank'] == 1)]
yearly_drivers_position2
| year | driverId | races_won | milliseconds | rank | |
|---|---|---|---|---|---|
| 3 | 1996 | 71 | 8 | 90738.000000 | 1.0 |
| 6 | 1997 | 35 | 7 | 80679.857143 | 1.0 |
| 12 | 1998 | 57 | 8 | 87343.625000 | 1.0 |
| 18 | 1999 | 57 | 5 | 85398.200000 | 1.0 |
| 22 | 2000 | 30 | 9 | 86048.111111 | 1.0 |
| 26 | 2001 | 30 | 9 | 87799.222222 | 1.0 |
| 32 | 2002 | 30 | 11 | 83060.727273 | 1.0 |
| 39 | 2003 | 30 | 6 | 76803.000000 | 1.0 |
| 44 | 2004 | 30 | 13 | 81564.076923 | 1.0 |
| 47 | 2005 | 8 | 7 | 85411.571429 | 1.0 |
| 51 | 2006 | 4 | 7 | 83043.285714 | 1.0 |
| 58 | 2007 | 8 | 6 | 86817.500000 | 1.0 |
| 65 | 2008 | 13 | 6 | 89610.166667 | 1.0 |
| 70 | 2009 | 18 | 6 | 87486.166667 | 1.0 |
| 77 | 2010 | 20 | 5 | 93325.800000 | 1.0 |
| 82 | 2011 | 20 | 11 | 94468.272727 | 1.0 |
| 89 | 2012 | 20 | 5 | 99101.800000 | 1.0 |
| 95 | 2013 | 20 | 13 | 95295.230769 | 1.0 |
| 96 | 2014 | 1 | 11 | 100653.090909 | 1.0 |
| 99 | 2015 | 1 | 10 | 96220.700000 | 1.0 |
| 102 | 2016 | 1 | 10 | 85645.500000 | 1.0 |
| 106 | 2017 | 1 | 9 | 92422.000000 | 1.0 |
| 111 | 2018 | 1 | 11 | 89360.818182 | 1.0 |
| 116 | 2019 | 1 | 11 | 86550.727273 | 1.0 |
| 121 | 2020 | 1 | 11 | 84877.545455 | 1.0 |
| 130 | 2021 | 830 | 10 | 92733.800000 | 1.0 |
| 133 | 2022 | 830 | 8 | 88567.625000 | 1.0 |
# joining the dataset to pull driver information into the final table
yearly_drivers_position3 = yearly_drivers_position2.merge(drivers_subset, on='driverId', how='left')
yearly_drivers_position3 = yearly_drivers_position3[['year','driverId','races_won','forename','surname']]
yearly_drivers_position3
| year | driverId | races_won | forename | surname | |
|---|---|---|---|---|---|
| 0 | 1996 | 71 | 8 | Damon | Hill |
| 1 | 1997 | 35 | 7 | Jacques | Villeneuve |
| 2 | 1998 | 57 | 8 | Mika | Häkkinen |
| 3 | 1999 | 57 | 5 | Mika | Häkkinen |
| 4 | 2000 | 30 | 9 | Michael | Schumacher |
| 5 | 2001 | 30 | 9 | Michael | Schumacher |
| 6 | 2002 | 30 | 11 | Michael | Schumacher |
| 7 | 2003 | 30 | 6 | Michael | Schumacher |
| 8 | 2004 | 30 | 13 | Michael | Schumacher |
| 9 | 2005 | 8 | 7 | Kimi | Räikkönen |
| 10 | 2006 | 4 | 7 | Fernando | Alonso |
| 11 | 2007 | 8 | 6 | Kimi | Räikkönen |
| 12 | 2008 | 13 | 6 | Felipe | Massa |
| 13 | 2009 | 18 | 6 | Jenson | Button |
| 14 | 2010 | 20 | 5 | Sebastian | Vettel |
| 15 | 2011 | 20 | 11 | Sebastian | Vettel |
| 16 | 2012 | 20 | 5 | Sebastian | Vettel |
| 17 | 2013 | 20 | 13 | Sebastian | Vettel |
| 18 | 2014 | 1 | 11 | Lewis | Hamilton |
| 19 | 2015 | 1 | 10 | Lewis | Hamilton |
| 20 | 2016 | 1 | 10 | Lewis | Hamilton |
| 21 | 2017 | 1 | 9 | Lewis | Hamilton |
| 22 | 2018 | 1 | 11 | Lewis | Hamilton |
| 23 | 2019 | 1 | 11 | Lewis | Hamilton |
| 24 | 2020 | 1 | 11 | Lewis | Hamilton |
| 25 | 2021 | 830 | 10 | Max | Verstappen |
| 26 | 2022 | 830 | 8 | Max | Verstappen |
# converting year from integer -> string
yearly_drivers_position3['year'] = yearly_drivers_position3['year'].astype(str)
yearly_drivers_position3.dtypes
year object driverId int64 races_won int64 forename object surname object dtype: object
import plotly.express as px
fig = px.bar(yearly_drivers_position3, x="year", y="races_won",facet_row_spacing = 1,facet_col_spacing=1,title='Maximun number of races won by a single driver across seasons')
fig.update_traces(width=0.5)
fig.update_traces(marker_color='blue')
fig.show()